<?php

/*-----------------------------------------------------------------------------------

	Custom Post/Portfolio Meta boxes

-----------------------------------------------------------------------------------*/



/*-----------------------------------------------------------------------------------*/
/*	Add metaboxes
/*-----------------------------------------------------------------------------------*/


function add_meta_gallery() {  
    add_meta_box(  
        'meta_gallery', // $id  
        __('Gallery', 'themeText'), // $title  
        'show_meta_gallery', // $callback  
        'post', // $page  
        'normal', // $context  
        'high'); // $priority
	
	
}  
add_action('add_meta_boxes', 'add_meta_gallery');

function add_meta_audio() {  
    add_meta_box(  
        'meta_audio', // $id  
        __('Audio', 'themeText'), // $title  
        'show_meta_audio', // $callback  
        'post', // $page  
        'normal', // $context  
        'high'); // $priority
	
}  
add_action('add_meta_boxes', 'add_meta_audio');

function add_meta_video() {  
    add_meta_box(  
        'meta_video', // $id  
        __('Video', 'themeText'), // $title  
        'show_meta_video', // $callback  
        'post', // $page  
        'normal', // $context  
        'high'); // $priority
	
}  
add_action('add_meta_boxes', 'add_meta_video');

function add_meta_quote() {  
    add_meta_box(  
        'meta_quote', // $id  
        __('Quote', 'themeText'), // $title  
        'show_meta_quote', // $callback  
        'post', // $page  
        'normal', // $context  
        'high'); // $priority
	
}  
add_action('add_meta_boxes', 'add_meta_quote');

function add_meta_link() {  
    add_meta_box(  
        'meta_link', // $id  
        __('Link', 'themeText'), // $title  
        'show_meta_link', // $callback  
        'post', // $page  
        'normal', // $context  
        'high'); // $priority
	
}  
add_action('add_meta_boxes', 'add_meta_link');


/*-----------------------------------------------------------------------------------*/
/*	Define fields
/*-----------------------------------------------------------------------------------*/

$prefix = '_sr';  


$meta_gallery = array(  
	array(  
	    'label' => __("Medias / Gallery", 'themeText'),  
	    'desc'  => __("Add images or embedded videos", 'themeText'),  
	    'id'    => $prefix.'_gallery',  
	    'type'  => 'medias'  
	)
 );


$meta_audio = array(  
	array(  
		"label" => __("MP3 File URL", 'themeText'),
	   	"desc" => __("The url to the mp3 file", 'themeText'),
	    'id'    => $prefix.'_mp3',  
	    'type'  => 'text'  
	),
	array(  
		"label" => __("OGA/OGG File URL", 'themeText'),
	   	"desc" => __("The url to the oga/ogg file", 'themeText'),
	    'id'    => $prefix.'_oga',  
	    'type'  => 'text'  
	)
 );


$meta_video = array(  
	array(  
		"label" => __("Embedded Video", 'themeText'),
	   	"desc" => __("Include the embedded iframe.", 'themeText'),
	    'id'    => $prefix.'_video',  
	    'type'  => 'textarea'  
	)
 );


$meta_quote = array(  
	array(  
		"label" => __("Quote", 'themeText'),
	   	"desc" => __("Enter the quote.", 'themeText'),
	    'id'    => $prefix.'_quote',  
	    'type'  => 'textarea'  
	)
 );


$meta_link = array(  
	array(  
		"label" => __("Link", 'themeText'),
	   	"desc" => __("Link url", 'themeText'),
	    'id'    => $prefix.'_link',  
	    'type'  => 'text'  
	)
 );


/*-----------------------------------------------------------------------------------*/
/*	Callback Show fields
/*-----------------------------------------------------------------------------------*/

function show_meta_gallery() {  
 	global $meta_gallery, $post;  
 	// Use nonce for verification  
 	echo '<input type="hidden" name="meta_gallery_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />';  
   
    // Begin the field table and loop  
    echo '<table class="form-table">';  
    foreach ($meta_gallery as $field) {  
    	// get value of this field if it exists for this post  
    	$meta = get_post_meta($post->ID, $field['id'], true);  
    	// begin a table row with  
    	echo '<tr> 
    			<th><label for="'.$field['id'].'">'.$field['label'].'</label><br /><span class="sr_description">'.$field['desc'].'</span></th> 
    			<td>';  
    			switch($field['type']) {  
                    
					// textarea
					case 'textarea':  
					    echo '<textarea name="'.$field['id'].'" id="'.$field['id'].'" cols="60" rows="4">'.$meta.'</textarea>';  
					break;
					
					// image  
					case 'image':  
					    
					    if ($meta) { $image = wp_get_attachment_image_src($meta, 'medium'); $image = $image[0]; }  else { $image = ''; } 
					    echo    '	<input class="add_featured_image button" type="button" value="'.__("Add Featured Image", 'themeText').'" />
									<input name="'.$field['id'].'" type="hidden" class="image_val" value="'.$meta.'" /> 
					                <small> <a href="#" class="remove-image">'.__("Remove Image", 'themeText').'</a></small> <br />
									<img src="'.$image.'" class="preview_image" alt="" /><br /> 
					                    ';  
					break;
					
					
					// gallery  
					case 'gallery':  
						echo '<input class="add_image button" type="button" value="'.__("Add Image", 'themeText').'" /> 
								<textarea name="'.$field['id'].'" style="display:none;" class="medias">'.$meta.'</textarea><br />
					            <ul id="sr_preview" class="preview">';
					    if ($meta) {
							$meta = substr($meta, 3);
							$meta = explode('~~~', $meta);
					        foreach($meta as $row) {
								$object = explode('~~', $row);
								$type = $object[0];
								$id = $object[1];
								$val = $object[2];
								if ($type == 'image') {
									$image = wp_get_attachment_image_src($val, 'medium'); $image = $image[0];
									//echo '<li><a class="image-remove button" href="#" rel="-'.$id.',">-</a> <span class="value"><img src="'.$image.'"></span></li>'; 
									echo '<li class="media-image"><a id="image-remove"  class="media-remove button" href="#" rel="'.$id.'">-</a> <span class="media-image"><img src="'.$image.'"></span><textarea id="hidden-value">~~~'.$row.'</textarea></li>';
								} else {
									echo '<li class="media-video"><a id="video-remove" class="media-remove button" href="#" rel="'.$id.'">-</a> <textarea id="video-code">'.$val.'</textarea><textarea id="hidden-value">~~~'.$row.'</textarea></li>';
								}
					        }  
					    }  
					    echo '</ul>';				
					break;
					
					
					// medias  
					case 'medias':  
						echo '<input class="add_image button" type="button" value="'.__("Add Image", 'themeText').'" /> 
								<input class="add_video button" type="button" value="'.__("Add Video", 'themeText').'" />
								<textarea name="'.$field['id'].'" style="display:none;" class="medias">'.$meta.'</textarea><br />
					            <ul id="sr_preview" class="preview">';
					    if ($meta) {
							$meta = substr($meta, 3);
							$meta = explode('~~~', $meta);
					        foreach($meta as $row) {
								$object = explode('~~', $row);
								$type = $object[0];
								$id = $object[1];
								$val = $object[2];
								if ($type == 'image') {
									$image = wp_get_attachment_image_src($val, 'medium'); $image = $image[0];
									//echo '<li><a class="image-remove button" href="#" rel="-'.$id.',">-</a> <span class="value"><img src="'.$image.'"></span></li>'; 
									echo '<li class="media-image"><a id="image-remove"  class="media-remove button" href="#" rel="'.$id.'">-</a> <span class="media-image"><img src="'.$image.'"></span><textarea id="hidden-value">~~~'.$row.'</textarea><a id="image-moveup"  class="button" href="#" >&uarr;</a><a id="image-movedown"  class="button" href="#" >&darr;</a></li>';
								} else {
									echo '<li class="media-video"><a id="video-remove" class="media-remove button" href="#" rel="'.$id.'">-</a> <textarea id="video-code">'.$val.'</textarea><textarea id="hidden-value">~~~'.$row.'</textarea><a id="image-moveup"  class="button" href="#" >&uarr;</a><a id="image-movedown"  class="button" href="#" >&darr;</a></li>';
								}
					        }  
					    }  
					    echo '</ul>';				
					break;
										
					 
                 } //end switch  
    	 echo '</td></tr>';  
	} // end foreach  
	echo '</table>'; // end table  
} 


function show_meta_audio() {  
 	global $meta_audio, $post;  
 	// Use nonce for verification  
 	echo '<input type="hidden" name="meta_audio_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />';  
   
    // Begin the field table and loop  
    echo '<table class="form-table">';  
    foreach ($meta_audio as $field) {  
    	// get value of this field if it exists for this post  
    	$meta = get_post_meta($post->ID, $field['id'], true);  
    	// begin a table row with  
    	echo '<tr> 
    			<th><label for="'.$field['id'].'">'.$field['label'].'</label><br /><span class="sr_description">'.$field['desc'].'</span></th> 
    			<td>';  
    			switch($field['type']) {  
                    
					// input text
    				case 'text':  
						echo '<input type="text" name="'.$field['id'].'" id="'.$field['id'].'" value="'.$meta.'" size="30" />';  
					break;
					
					// textarea
					case 'textarea':  
					    echo '<textarea name="'.$field['id'].'" id="'.$field['id'].'" cols="60" rows="4">'.$meta.'</textarea>';  
					break;					
					 
                 } //end switch  
    	 echo '</td></tr>';  
	} // end foreach  
	echo '</table>'; // end table  
} 


function show_meta_video() {  
 	global $meta_video, $post;  
 	// Use nonce for verification  
 	echo '<input type="hidden" name="meta_video_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />';  
   
    // Begin the field table and loop  
    echo '<table class="form-table">';  
    foreach ($meta_video as $field) {  
    	// get value of this field if it exists for this post  
    	$meta = get_post_meta($post->ID, $field['id'], true);  
    	// begin a table row with  
    	echo '<tr> 
    			<th><label for="'.$field['id'].'">'.$field['label'].'</label><br /><span class="sr_description">'.$field['desc'].'</span></th> 
    			<td>';  
    			switch($field['type']) {  
                    
					// input text
    				case 'text':  
						echo '<input type="text" name="'.$field['id'].'" id="'.$field['id'].'" value="'.$meta.'" size="30" />';  
					break;
					
					// textarea
					case 'textarea':  
					    echo '<textarea name="'.$field['id'].'" id="'.$field['id'].'" cols="60" rows="4">'.$meta.'</textarea>';  
					break;					
					 
                 } //end switch  
    	 echo '</td></tr>';  
	} // end foreach  
	echo '</table>'; // end table  
} 


function show_meta_quote() {  
 	global $meta_quote, $post;  
 	// Use nonce for verification  
 	echo '<input type="hidden" name="meta_quote_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />';  
   
    // Begin the field table and loop  
    echo '<table class="form-table">';  
    foreach ($meta_quote as $field) {  
    	// get value of this field if it exists for this post  
    	$meta = get_post_meta($post->ID, $field['id'], true);  
    	// begin a table row with  
    	echo '<tr> 
    			<th><label for="'.$field['id'].'">'.$field['label'].'</label><br /><span class="sr_description">'.$field['desc'].'</span></th> 
    			<td>';  
    			switch($field['type']) {  
                    
					// input text
    				case 'text':  
						echo '<input type="text" name="'.$field['id'].'" id="'.$field['id'].'" value="'.$meta.'" size="30" />';  
					break;
					
					// textarea
					case 'textarea':  
					    echo '<textarea name="'.$field['id'].'" id="'.$field['id'].'" cols="60" rows="4">'.$meta.'</textarea>';  
					break;					
					 
                 } //end switch  
    	 echo '</td></tr>';  
	} // end foreach  
	echo '</table>'; // end table  
}


function show_meta_link() {  
 	global $meta_link, $post;  
 	// Use nonce for verification  
 	echo '<input type="hidden" name="meta_link_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />';  
   
    // Begin the field table and loop  
    echo '<table class="form-table">';  
    foreach ($meta_link as $field) {  
    	// get value of this field if it exists for this post  
    	$meta = get_post_meta($post->ID, $field['id'], true);  
    	// begin a table row with  
    	echo '<tr> 
    			<th><label for="'.$field['id'].'">'.$field['label'].'</label><br /><span class="sr_description">'.$field['desc'].'</span></th> 
    			<td>';  
    			switch($field['type']) {  
                    
					// input text
    				case 'text':  
						echo '<input type="text" name="'.$field['id'].'" id="'.$field['id'].'" value="'.$meta.'" size="30" />';  
					break;
					
					// textarea
					case 'textarea':  
					    echo '<textarea name="'.$field['id'].'" id="'.$field['id'].'" cols="60" rows="4">'.$meta.'</textarea>';  
					break;					
					 
                 } //end switch  
    	 echo '</td></tr>';  
	} // end foreach  
	echo '</table>'; // end table  
}
/*-----------------------------------------------------------------------------------*/
/*	Save Datas
/*-----------------------------------------------------------------------------------*/

function save_meta_gallery($post_id) {  
    global $meta_gallery;  
  
    // verify nonce  
    if (!isset($_POST['meta_gallery_nonce']) || !wp_verify_nonce($_POST['meta_gallery_nonce'], basename(__FILE__))) 
        return $post_id; 
		
    // check autosave  
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)  
        return $post_id;
		
    // check permissions  
    if ('page' == $_POST['post_type']) {  
        if (!current_user_can('edit_page', $post_id))  
            return $post_id;  
        } elseif (!current_user_can('edit_post', $post_id)) {  
            return $post_id;  
    }  
  
    // loop through fields and save the data  
    foreach ($meta_gallery as $field) {  
        $old = get_post_meta($post_id, $field['id'], true);  
        $new = $_POST[$field['id']];  
        if ($new && $new != $old) {  
            update_post_meta($post_id, $field['id'], $new);  
        } elseif ('' == $new && $old) {  
            delete_post_meta($post_id, $field['id'], $old);  
        }  
    } // end foreach  
}  
add_action('save_post', 'save_meta_gallery');



function save_meta_audio($post_id) {  
    global $meta_audio;  
  
    // verify nonce  
    if (!isset($_POST['meta_audio_nonce']) || !wp_verify_nonce($_POST['meta_audio_nonce'], basename(__FILE__))) 
        return $post_id; 
		
    // check autosave  
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)  
        return $post_id;
		
    // check permissions  
    if ('page' == $_POST['post_type']) {  
        if (!current_user_can('edit_page', $post_id))  
            return $post_id;  
        } elseif (!current_user_can('edit_post', $post_id)) {  
            return $post_id;  
    }  
  
    // loop through fields and save the data  
    foreach ($meta_audio as $field) {  
        $old = get_post_meta($post_id, $field['id'], true);  
        $new = $_POST[$field['id']];  
        if ($new && $new != $old) {  
            update_post_meta($post_id, $field['id'], $new);  
        } elseif ('' == $new && $old) {  
            delete_post_meta($post_id, $field['id'], $old);  
        }  
    } // end foreach  
}  
add_action('save_post', 'save_meta_audio');



function save_meta_video($post_id) {  
    global $meta_video;  
  
    // verify nonce  
    if (!isset($_POST['meta_video_nonce']) || !wp_verify_nonce($_POST['meta_video_nonce'], basename(__FILE__))) 
        return $post_id; 
		
    // check autosave  
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)  
        return $post_id;
		
    // check permissions  
    if ('page' == $_POST['post_type']) {  
        if (!current_user_can('edit_page', $post_id))  
            return $post_id;  
        } elseif (!current_user_can('edit_post', $post_id)) {  
            return $post_id;  
    }  
  
    // loop through fields and save the data  
    foreach ($meta_video as $field) {  
        $old = get_post_meta($post_id, $field['id'], true);  
        $new = $_POST[$field['id']];  
        if ($new && $new != $old) {  
            update_post_meta($post_id, $field['id'], $new);  
        } elseif ('' == $new && $old) {  
            delete_post_meta($post_id, $field['id'], $old);  
        }  
    } // end foreach  
}  
add_action('save_post', 'save_meta_video');



function save_meta_quote($post_id) {  
    global $meta_quote;  
  
    // verify nonce  
    if (!isset($_POST['meta_quote_nonce']) || !wp_verify_nonce($_POST['meta_quote_nonce'], basename(__FILE__))) 
        return $post_id; 
		
    // check autosave  
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)  
        return $post_id;
		
    // check permissions  
    if ('page' == $_POST['post_type']) {  
        if (!current_user_can('edit_page', $post_id))  
            return $post_id;  
        } elseif (!current_user_can('edit_post', $post_id)) {  
            return $post_id;  
    }  
  
    // loop through fields and save the data  
    foreach ($meta_quote as $field) {  
        $old = get_post_meta($post_id, $field['id'], true);  
        $new = $_POST[$field['id']];  
        if ($new && $new != $old) {  
            update_post_meta($post_id, $field['id'], $new);  
        } elseif ('' == $new && $old) {  
            delete_post_meta($post_id, $field['id'], $old);  
        }  
    } // end foreach  
}  
add_action('save_post', 'save_meta_quote');



function save_meta_link($post_id) {  
    global $meta_link;  
  
    // verify nonce  
    if (!isset($_POST['meta_link_nonce']) || !wp_verify_nonce($_POST['meta_link_nonce'], basename(__FILE__))) 
        return $post_id; 
		
    // check autosave  
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)  
        return $post_id;
		
    // check permissions  
    if ('page' == $_POST['post_type']) {  
        if (!current_user_can('edit_page', $post_id))  
            return $post_id;  
        } elseif (!current_user_can('edit_post', $post_id)) {  
            return $post_id;  
    }  
  
    // loop through fields and save the data  
    foreach ($meta_link as $field) {  
        $old = get_post_meta($post_id, $field['id'], true);  
        $new = $_POST[$field['id']];  
        if ($new && $new != $old) {  
            update_post_meta($post_id, $field['id'], $new);  
        } elseif ('' == $new && $old) {  
            delete_post_meta($post_id, $field['id'], $old);  
        }  
    } // end foreach  
}  
add_action('save_post', 'save_meta_link');

/*-----------------------------------------------------------------------------------*/
/*	Register and load function javascript
/*-----------------------------------------------------------------------------------*/

if( !function_exists( 'sr_admin_meta_js' ) ) {
    function sr_admin_meta_js($hook) {
		global $pagenow;

		wp_register_script('functions-script', get_template_directory_uri() . '/functions/js/functions_script.js', 'jquery');
		wp_enqueue_script('functions-script');
		
		if ( (isset($_GET['post']) && $_GET['action'] == 'edit' && get_post_type( $_GET['post'] )  == 'post' ) || ($pagenow == 'post-new.php' && !isset($_GET['post_type'])) ) {
			wp_register_script('customfields-script', get_template_directory_uri() . '/functions/js/customfields_script.js', 'jquery');
			wp_enqueue_script('customfields-script');
		}
		
		wp_register_style('functions-style', get_template_directory_uri() . '/functions/css/functions.css');
		wp_enqueue_style('functions-style');
    }
    
    add_action('admin_enqueue_scripts','sr_admin_meta_js',10,1);
}


?>